package com.viego.financialrecommendsystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.viego.financialrecommendsystem.common.*;
import com.viego.financialrecommendsystem.entity.AccessRecord;
import com.viego.financialrecommendsystem.entity.MyOrder;
import com.viego.financialrecommendsystem.entity.Product;
import com.viego.financialrecommendsystem.service.IOrderService;
import com.viego.financialrecommendsystem.service.IProductService;
import com.viego.financialrecommendsystem.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    IProductService iProductService;

    @Resource
    IOrderService orderService;

    @Resource
    IUserService userService;


    /**
     * 产品的分页查询
     * @param pageRequest 封装好的分页查询请求体
     * @return 封装好的 MyPage 对象
     */
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PageRequest<Product> pageRequest){
        log.info("当前用户 UID = {} 正在进行查询", BaseContext.getCurrentId());
        Page page = iProductService.page(pageRequest.getCurrentPage(),
                pageRequest.getPageSize(), pageRequest.getObject());
        MyPage<Product> myPage = new MyPage<Product>(
                pageRequest.getCurrentPage(),
                pageRequest.getPageSize(),
                (int) page.getPages(),
                page.getRecords());
        return AjaxResult.success("查询成功",myPage);
    }

    /**
     * 产品的推荐方法
     * @param guids 参数
     * @return 返回一个适合用户的产品
     */
    @PostMapping("/recommend")
    public AjaxResult recommend(@RequestBody List<Guid> guids){
        List<Plan> plans = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();//获取当前用户的id
        /*------------------------------------------------------------------------------------------------------------------------------*/
        /*-接下来，计算 年限 <===>  金额-*/
        List<MyOrder> orderList = orderService.getAllOrdersByUID(currentId);
        int year = 0; int target = 0;
        String selected = guids.get(2).getSelected();
        String str = "大于";
        if (orderList.size() != 0){
             year = orderList.stream().mapToInt(MyOrder::getYear).sum()/orderList.size();//根据订单 => 推算出用户的 平均年限
             target = orderList.stream().mapToInt(MyOrder::getAmount).sum()/orderList.size();//根据订单 => 推算出用户的 平均金额
        }else{
            str = selected.substring(0,2);
        }
            year = year  + Integer.parseInt( guids.get(1).getSelected().substring(0,1));
            year /= 2;
            target = Integer.parseInt(selected.substring(2)) + target;
            target /= 2;

        /*------------------------------------------------------------------------------------------------------------------------------*/
        /*-接下来，计算适合的产品名称-*/
        String type1 = "";
        String type2 = "";
        List<String> list = userService.getAllAccessRecord(currentId);//该用户经常访问的两类金融产品
        if (list.size() != 0){
            type1 = list.get(0);type2 = list.get(1);
            Product product1 = iProductService.getOneByType(type1);
            Product product2 = iProductService.getOneByType(type2);
            Plan plan1 = new Plan();
            plan1.setProductName(product1.getProductName());
            plan1.setAmount(getAmount(str,target));
            plan1.setYear(year);
            Plan plan2 = new Plan();
            plan2.setProductName(product2.getProductName());
            plan2.setAmount(getAmount(str,target));
            plan2.setYear(year);
            plans.add(plan1);
            plans.add(plan2);
        }
        //如果数据库中，没有查到用户的访问记录，那就根据 guid 进行查询
        String[] split = guids.get(0).getSelected().split("/");
        type1 = split[0];type2 = split[1];
        Product product1 = iProductService.getOneByType(type1);
        Product product2 = iProductService.getOneByType(type2);
        Plan plan1 = new Plan();
        plan1.setProductName(product1.getProductName());
        plan1.setAmount(getAmount(str,target));
        plan1.setYear(year);
        Plan plan2 = new Plan();
        plan2.setProductName(product2.getProductName());
        plan2.setAmount(getAmount(str,target));
        plan2.setYear(year);
        plans.add(plan1);
        plans.add(plan2);
        return AjaxResult.success("分析成功",plans);
    }

    @GetMapping("/hot")
    public AjaxResult hot(){
        List<Product> list = iProductService.hot();
        return AjaxResult.success("查询成功",list);
    }


    private static int getAmount(String str, int target) {
        Random random = new Random();
        int b = target / 1000;
        int a = b - 5;
       switch (str){
           case "大于":
              return 1000 * random.nextInt((b + 5) - b + 1) + b;
           case "小于":
               return 1000 * random.nextInt(b - a + 1) + a;
           default:
               return 0;
       }
    }

}
